using System;
using System.IO;
using Widgetsphere.Core.Logging;
using System.Windows.Forms;

namespace Widgetsphere.Core.Util
{
	public partial class FileSystemHelper
	{
		#region Class's Log Switch
		private static readonly TraceSwitch _traceSwitch = LogClass.WSCommonUtil;
		#endregion

		public FileSystemHelper()
		{
		}

		public static bool ValidDirectoryString(string directoryPath)
		{
			try
			{
				DirectoryInfo currentFolder = new DirectoryInfo(directoryPath);
				return true;
			}
			catch(Exception ex)
			{
				MessageLog.LogWarning(_traceSwitch, ex);
				return false;
			}
		}

		public static string DirectoryFromFileName(string fileName)
		{
			FileInfo fi =new FileInfo(fileName);
			return fi.DirectoryName;
		}

    #region SaveFile

    public static bool SaveFile(string path, string text)
    {
      try
      {
        //Remove the file if necessary
        if(System.IO.File.Exists(path))
          System.IO.File.Delete(path);

        System.IO.StreamWriter sw = System.IO.File.AppendText(path);
        sw.Write(text);
        sw.Close();
        return true;
      }
      catch(Exception ex)
      {
        throw;
      }
    }

    #endregion

    #region LoadFile

    public static string LoadFile(string path)
    {
      try
      {
        System.IO.StreamReader sw = System.IO.File.OpenText(path);
        string retval = sw.ReadToEnd();
        sw.Close();
        return retval;
      }
      catch(Exception ex)
      {
        throw;
      }
    }

    #endregion

    #region ShowDialogOpenFile

    public static string ShowDialogOpenFile(string title, string filter)
    {
      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.Title = title;
      openFileDialog.Filter = filter;
      if(openFileDialog.ShowDialog() == DialogResult.OK)
      {
        return openFileDialog.FileName;
      }
      return "";
    }

    #endregion

    #region ShowDialogSaveFile

    public static string ShowDialogSaveFile(string title, string filter)
    {
      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.Title = title;
      saveFileDialog.Filter = filter;
      if(saveFileDialog.ShowDialog() == DialogResult.OK)
      {
        return saveFileDialog.FileName;
      }
      return "";
    }

    #endregion

	}
}
